package com.yby6.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yby6.common.core.controller.BaseController;
import com.yby6.system.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yby6.common.core.AjaxResult;
import com.yby6.common.core.domain.SysRole;
import com.yby6.common.core.page.TableDataInfo;
import com.yby6.common.satoken.LoginHelper;

/**
 * ClassName: SysRoleController
 *
 * @author yangshuai
 * @Date: 2021-05-19 13:17
 * @Description: 角色管理 $
 **/
@RestController
@RequestMapping("/system/sysRole")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 分页查询角色列表
     * @param sysRole
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(SysRole sysRole) {
        startPage();
        return getDataTable(sysRoleService.list(sysRole));
    }

    /**
     * 根据ID查询角色信息(权限回显)
     * @param sysRole
     * @return
     */
    @GetMapping("/getRoleById/{id}")
    public AjaxResult getRoleById(@PathVariable Long id) {
        return AjaxResult.success().addParam(this.sysRoleService.getRoleById(id));
    }

    /**
     * 查询全部角色信息（下拉树）
     * @return
     */
    @GetMapping("/getRoleAll")
    public AjaxResult getRoleAll(){
        return AjaxResult.success().addParam(this.sysRoleService.getRoleAll());
    }

    /**
     * 新增角色信息
     * @param sysRole
     * @return
     */
    @PostMapping
    public AjaxResult add(@RequestBody SysRole sysRole) {
        sysRole.setCreateBy(LoginHelper.getUsername());
        sysRole.setUpdateBy(sysRole.getCreateBy());
        return toAjax(this.sysRoleService.add(sysRole));
    }

    /**
     * 更新角色信息
     * @param sysRole
     * @return
     */
    @PutMapping
    public AjaxResult update(@RequestBody SysRole sysRole) {
        sysRole.setUpdateBy(LoginHelper.getUsername());
        return toAjax(this.sysRoleService.update(sysRole));
    }

    /**
     * 删除角色信息
     * @param sysRole
     * @return
     */
    @DeleteMapping("/{ids}")
    @SaCheckPermission("sys:role:delete")
    public AjaxResult delete(@PathVariable Long[] ids) {
        return toAjax(this.sysRoleService.delete(ids));
    }


}